package com.elsa.smart_buffet.service.impl;

import com.alibaba.fastjson.JSON;
import com.elsa.smart_buffet.mapper.MenuMapper;
import com.elsa.smart_buffet.mapper.MenuOrderMapper;
import com.elsa.smart_buffet.mapper.OrderMapper;
import com.elsa.smart_buffet.pojo.Menu;
import com.elsa.smart_buffet.pojo.MenuOrder;
import com.elsa.smart_buffet.pojo.Order;
import com.elsa.smart_buffet.pojo.ResultBox.CallBackMessage;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.pojo.vo.OrderInfoStats;
import com.elsa.smart_buffet.service.MessageService;
import com.elsa.smart_buffet.service.OrderService;
import com.elsa.smart_buffet.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MessageService messageService;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public ResponseResult insertOrder(List<Map<String, Object>> list) throws InterruptedException {

        double totalPrice = 0.0;

        MenuOrder menuOrder = new MenuOrder();
        Order order = new Order();

        String orderId = Integer.toString(orderMapper.selectCount(null) + 1);

        for (Map<String, Object> stringObjectMap : list) {

            String cId = stringObjectMap.get("cId").toString();
            menuOrder.setCId(Integer.parseInt(cId));

            menuOrder.setOId(orderId);

            String moNum = stringObjectMap.get("moNum").toString();
            menuOrder.setMoNum(Integer.parseInt(moNum));

            String mId = stringObjectMap.get("mId").toString();

            Menu menu = menuMapper.selectById(mId);
            Double moPrice = menu.getMPrice();
            menuOrder.setMoPrice(moPrice);

            totalPrice += menuOrder.getMoPrice() * Integer.parseInt(moNum);

        }
        System.out.println(totalPrice);
        log.info("totalPrice:" + totalPrice);

        order.setCId(menuOrder.getCId());
        order.setOId(orderId);
        order.setOTotalPrice(totalPrice);
        order.setOStatus(1);
        Date date = new Date(DateUtil.now().getTime());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Date ctime2 = new java.sql.Date(new java.util.Date().getTime());
        order.setOTime(date);
        order.setOStringTime(df.format(ctime2));
        System.out.println(order);
        orderMapper.insert(order);

        for (Map<String, Object> stringObjectMap : list) {

            menuOrder.setOId(orderId);

            String mId = stringObjectMap.get("mId").toString();
            menuOrder.setMId(Integer.parseInt(mId));

            Menu menu = menuMapper.selectById(mId);

            String cId = stringObjectMap.get("cId").toString();
            menuOrder.setCId(Integer.parseInt(cId));

            menuOrder.setMName(menu.getMName());

            String moNum = stringObjectMap.get("moNum").toString();
            menuOrder.setMoNum(Integer.parseInt(moNum));

//            String moPrice = stringObjectMap.get("moPrice").toString();
            menuOrder.setMoPrice(menu.getMPrice());

            System.out.println(menuOrder);
            menuOrderMapper.insert(menuOrder);

        }

        // System.out.println("订单开始统计数据处理");
        log.info("订单开始统计数据处理，订单编号:" + menuOrder.getOId());

        try {
            if(orderMapper.selectById(menuOrder.getOId()) == null) {
                log.info("订单表还没存入数据");
                Thread.sleep(500);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }


        // 订单消息处理
        messageService.sendMessage(menuOrder.getOId());

        // System.out.println("订单结束统计数据处理");
        log.info("订单结束统计数据处理，订单编号:" + menuOrder.getOId());

        return new ResponseResult(CallBackMessage.SUCCESS, "成功保存订单数据");
    }

    @Override
    public ResponseResult insertOrder(MenuOrder menuOrder) {

        double totalPrice = 0;
        totalPrice += menuOrder.getMoPrice() * menuOrder.getMoNum();

        Order order = new Order();
        order.setCId(menuOrder.getCId());
        order.setOId(menuOrder.getOId());
        order.setOTotalPrice(totalPrice);
        order.setOStatus(1);
        Date date = new Date(DateUtil.now().getTime());
        order.setOTime(date);
        orderMapper.insert(order);

        menuOrderMapper.insert(menuOrder);

        // 订单消息处理
        messageService.sendMessage(menuOrder.getOId());

        // System.out.println("订单结束统计数据处理");
        log.info("订单结束统计数据处理，订单编号:" + menuOrder.getOId());
        return new ResponseResult(CallBackMessage.SUCCESS, "成功保存订单数据");
    }

    @Override
    public ResponseResult selectAllOrder() {
        log.info(String.valueOf(orderMapper.selectList(null)));
        return new ResponseResult(CallBackMessage.SUCCESS, orderMapper.selectList(null));
    }

    @Override
    public ResponseResult selectAllOrderInOrderById() {
        log.info("全部订单信息：" + orderMapper.selectAllOrderInOrderById());
        return new ResponseResult(CallBackMessage.SUCCESS, orderMapper.selectAllOrderInOrderById());
    }

    @Override
    public ResponseResult selectOrderByDate(String time) {
        String[] SplitTime = time.split("\\-");
        for(int i = 0; i < SplitTime.length; i++) {
            SplitTime[i] = SplitTime[i].replace("/", "-");
            log.info(SplitTime[i]);
        }
        return new ResponseResult(CallBackMessage.SUCCESS, orderMapper.selectOrderInOrderByTime(SplitTime[0], SplitTime[1]));
    }

    @Override
    public ResponseResult selectOrderByOId(String oId) {
        return new ResponseResult(CallBackMessage.SUCCESS, orderMapper.selectById(oId));
    }

    @Override
    public ResponseResult selectOrderByConsumerName(String name) {
        return new ResponseResult(CallBackMessage.SUCCESS, orderMapper.selectOrderByConsumerName(name));
    }

    @Override
    public ResponseResult selectOrderByCId(String CId) {
        List<Order> orderList = orderMapper.selectOrderByCId(CId);
        List<OrderInfoStats> orderInfoStatsList = new ArrayList<>();
        for(int i = 0; i < orderList.size(); i++) {
            OrderInfoStats orderInfoStats = new OrderInfoStats();
            orderInfoStats.setCId(orderList.get(i).getCId());
            orderInfoStats.setOStatus(orderList.get(i).getOStatus());
            orderInfoStats.setOId(orderList.get(i).getOId());
            orderInfoStats.setOTime(orderList.get(i).getOTime());
            orderInfoStats.setOStringTime(orderList.get(i).getOStringTime());
            orderInfoStats.setOTotalPrice(orderList.get(i).getOTotalPrice());
            List<MenuOrder> menuOrderList = new ArrayList<>();
            String menu = "";
            menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrder(orderList.get(i).getOId());
            for(int j = 0; j < menuOrderList.size(); j++) {
                if(j != menuOrderList.size() - 1) {
                    menu += menuOrderList.get(j).getMName() + "、";
                } else {
                    menu += menuOrderList.get(j).getMName();
                }
            }
            orderInfoStats.setMenu(menu);
            orderInfoStatsList.add(orderInfoStats);
        }
        log.info(String.valueOf(orderInfoStatsList));
        return new ResponseResult(CallBackMessage.SUCCESS, orderInfoStatsList);
    }

    @Override
    public ResponseResult selectOrderByOIdWithMenu(String OId) {
        Order order = orderMapper.selectById(OId);
        OrderInfoStats orderInfoStats = new OrderInfoStats();
        orderInfoStats.setOStatus(order.getOStatus());
        orderInfoStats.setOTotalPrice(order.getOTotalPrice());
        orderInfoStats.setOStringTime(order.getOStringTime());
        orderInfoStats.setOId(order.getOId());
        String menu = "";
        List<MenuOrder> menuOrderList;
        menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrder(OId);
        for(int j = 0; j < menuOrderList.size(); j++) {
            if(j != menuOrderList.size() - 1) {
                menu += menuOrderList.get(j).getMName() + "、";
            } else {
                menu += menuOrderList.get(j).getMName();
            }
        }
        orderInfoStats.setMenu(menu);

        return new ResponseResult(CallBackMessage.SUCCESS, orderInfoStats);
    }
}
